package com.student.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student.demo.config.Result;
import com.student.demo.entity.User;
import com.student.demo.mapper.UserMapper;
import com.student.demo.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public Result<?> login(HttpServletRequest request) {
        User user = new User();
        user.setUserName(request.getParameter("userName"));
        user.setPassword(request.getParameter("password"));
        if (verifyAccount(user.getUserName(), user.getPassword())){
            request.getSession().setAttribute("pass",true);
        } else {
            request.getSession().invalidate(); // 作废当前session
            return Result.error("用户名或密码错误");
        }
        return Result.success();
    }

    /**
     * 验证账号
     * @param userName
     * @param password
     * @return
     */
    private boolean verifyAccount(String userName, String password){
        long count = query().eq("user_name",userName).eq("password",password).count();
        if (count > 0){
            return true;
        }
        return false;
    }
}
